package org.zhq.service.impl.center;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.zhq.enums.OrderStatusEnum;
import org.zhq.enums.YesOrNo;
import org.zhq.mapper.OrderStatusMapper;
import org.zhq.mapper.OrdersMapper;
import org.zhq.mapper.OrdersMapperCustom;
import org.zhq.pojo.OrderStatus;
import org.zhq.pojo.Orders;
import org.zhq.pojo.vo.MyOrdersVO;
import org.zhq.pojo.vo.OrderStatusCountsVO;
import org.zhq.service.center.MyOrdersService;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class MyOrdersServiceImpl implements MyOrdersService {

    public OrdersMapperCustom ordersMapperCustom;

    public OrdersMapper ordersMapper;

    public OrderStatusMapper orderStatusMapper;

    @Autowired
    public MyOrdersServiceImpl(OrdersMapperCustom ordersMapperCustom, OrdersMapper ordersMapper, OrderStatusMapper orderStatusMapper) {
        this.ordersMapperCustom = ordersMapperCustom;
        this.ordersMapper = ordersMapper;
        this.orderStatusMapper = orderStatusMapper;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public OrderStatusCountsVO getOrderStatusCounts(String userId) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId",userId);
        map.put("orderStatus",OrderStatusEnum.WAIT_PAY.type);
        int waitPayCount = ordersMapperCustom.getMyOrderStatusCounts(map);
        map.put("orderStatus",OrderStatusEnum.WAIT_DELIVER.type);
        int waitDeliverCount = ordersMapperCustom.getMyOrderStatusCounts(map);
        map.put("orderStatus",OrderStatusEnum.WAIT_RECEIVE.type);
        int waitReceiveCount = ordersMapperCustom.getMyOrderStatusCounts(map);
        map.put("orderStatus",OrderStatusEnum.SUCCESS.type);
        map.put("isComment",YesOrNo.NO.type);
        int waitCommentCount = ordersMapperCustom.getMyOrderStatusCounts(map);
        OrderStatusCountsVO orderStatusCountsVO = new OrderStatusCountsVO();
        orderStatusCountsVO.setWaitPayCounts(waitPayCount);
        orderStatusCountsVO.setWaitDeliverCounts(waitDeliverCount);
        orderStatusCountsVO.setWaitReceiveCounts(waitReceiveCount);
        orderStatusCountsVO.setWaitCommentCounts(waitCommentCount);
        return orderStatusCountsVO;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public IPage<MyOrdersVO> queryMyOrders(String userId, Integer orderStatus, Integer pageNo, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        if (orderStatus != null) {
            map.put("orderStatus", orderStatus);
        }
        Page page = new Page(pageNo, pageSize);
        return ordersMapperCustom.queryMyOrders(page,map);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public void updateDeliverOrderStatus(String orderId) {
        OrderStatus updateOrderStatus = new OrderStatus();
        updateOrderStatus.setOrderId(orderId);
        updateOrderStatus.setDeliverTime(new Date());
        updateOrderStatus.setOrderStatus(OrderStatusEnum.WAIT_RECEIVE.type);
        LambdaQueryWrapper<OrderStatus> condition = Wrappers.<OrderStatus>lambdaQuery()
                .eq(OrderStatus::getOrderId, orderId)
                .eq(OrderStatus::getOrderStatus, OrderStatusEnum.WAIT_DELIVER.type);
        orderStatusMapper.update(updateOrderStatus, condition);
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean updateReceiveOrderStatus(String orderId) {
        OrderStatus updateOrderStatus = new OrderStatus();
        updateOrderStatus.setOrderId(orderId);
        updateOrderStatus.setSuccessTime(new Date());
        updateOrderStatus.setOrderStatus(OrderStatusEnum.SUCCESS.type);
        LambdaQueryWrapper<OrderStatus> condition = Wrappers.<OrderStatus>lambdaQuery()
                .eq(OrderStatus::getOrderId, orderId)
                .eq(OrderStatus::getOrderStatus, OrderStatusEnum.WAIT_DELIVER.type);
        return orderStatusMapper.update(updateOrderStatus, condition) > 0;
    }

    @Transactional(propagation = Propagation.REQUIRED)
    @Override
    public boolean deleteOrder(String userId, String orderId) {
        Orders updateOrder = new Orders();
        updateOrder.setUserId(userId);
        updateOrder.setId(orderId);
        updateOrder.setIsDelete(YesOrNo.YES.type);
        updateOrder.setUpdatedTime(new Date());
        return ordersMapper.updateById(updateOrder) > 0;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public Orders queryMyOrder(String userId, String orderId) {
        LambdaQueryWrapper<Orders> condition = Wrappers.<Orders>lambdaQuery()
                .eq(Orders::getUserId, userId)
                .eq(Orders::getId, orderId)
                .eq(Orders::getIsDelete, YesOrNo.NO.type);
        return ordersMapper.selectOne(condition);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public IPage<OrderStatus> getOrdersTrend(String userId, Integer pageNo, Integer pageSize) {
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        Page page = new Page(pageNo,pageSize);
        return ordersMapperCustom.getMyOrderTrend(page,map);
    }
}
